package cn.myzf.fort.access.accesscommon.core;




import cn.myzf.fort.access.accesscommon.core.impl.CustomStreamingProcess;
import cn.myzf.fort.access.accesscommon.processoutput.output.StreamingProcessOutput;

import java.util.List;
import java.util.logging.Logger;


/**
 * 获取执行参数args
 * */

public abstract class AbstractBinary {

    protected static Logger log = Logger.getLogger(AbstractBinary.class.getName());


    public abstract List<String> commandLineArgs() throws Exception;


    public AbstractBinary() {
        reset();
    }

    public void reset() {

    }

    protected String foward_info;

    public abstract Boolean checkOccupancy() throws Exception;



    public ProcessBuilder builder() throws Exception {
        ProcessBuilder builder;
        List<String> args;
        if (!checkOccupancy()) throw new IllegalStateException("port forward is occupancy!");
        args = commandLineArgs();

        log.info("Command-line: " + Utils.flatten(args, " "));

        builder = new ProcessBuilder();
        builder.command(args);
        return builder;

    }

    public Process start() throws Exception {
        return builder().start();
    }



    public StreamingProcessOutput execute() throws Exception {
        StreamingProcessOutput result;


        result = new StreamingProcessOutput(new CustomStreamingProcess(foward_info));
        result.monitor(builder());


        return result;

    }


}
